package com.framework.seedkit.utils.encrypt;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class MD5 {
	//混淆因子
	public final static String key = "00CBA177EA725E7C";
	//TAG
	private static final String TAG = MD5.class.getSimpleName();

	/**
	 * 获取字符串形式的MD5值
	 */
	private static String md5(String source) {
		String MD5Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(source.getBytes("utf-8"));
			md.update(array);
			MD5Str = HexUtil.hex(md.digest());
			return MD5Str;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 描述:获取MD5签名值前,使用key作为干扰因子混淆MD5值
	 */
	public static String FroadMallMD5(String body) {
		return md5(body + key);
	}

	/**
	 * 描述: 根据文件路径得到文件的MD5签名值
	 */
	public static String getPackageMd5(String path){
	    byte[] buffer = new byte[1024];
	    int numRead = 0;
		InputStream fis;
		MessageDigest md5;
	    String MDStr = null;

	    try{
	        fis = new FileInputStream(path);
	        md5 = MessageDigest.getInstance("MD5");
	        while((numRead=fis.read(buffer)) > 0) {
	            md5.update(buffer,0,numRead);
	        }
	        fis.close();
			MDStr = HexUtil.hex(md5.digest());
	    } catch(Exception e) {
	        e.printStackTrace();
	    }

	    return MDStr;
	}
}
